package name.raynal.tool;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class SebZipFile extends File {

	private static final long serialVersionUID = 5557683598281059345L;

	public SebZipFile(String pathname) {
		super(pathname);
	}

	public File extractZipEntry(String entryName, String outputDirPath) {
		final int BUFFER = 256;
		File output = null;
		BufferedOutputStream dest;

		try {
			FileInputStream fis = new FileInputStream(this);
			ZipInputStream zis = new ZipInputStream(
					new BufferedInputStream(fis));
			ZipEntry entry;

			while ((entry = zis.getNextEntry()) != null) {
				int count;
				byte data[] = new byte[BUFFER];
				if (entry.getName().matches(entryName)) {
					System.out.println("Extracting: " + entry);
					output = new File(outputDirPath + '/' + entry.getName());
					FileOutputStream fos = new FileOutputStream(output);
					dest = new BufferedOutputStream(fos, BUFFER);
					while ((count = zis.read(data, 0, BUFFER)) != -1) {
						dest.write(data, 0, count);
					}
					dest.flush();
					dest.close();
					break;
				}

			}
			zis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return output;
	}
}
